package com.iemes.controller.craft;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.BrandFormMap;
import com.iemes.entity.BrandModelFormMap;
import com.iemes.entity.ClientFormMap;
import com.iemes.entity.CraftfourTimeFormMap;
import com.iemes.entity.CraftoneTimeFormMap;
import com.iemes.entity.CraftplaceFormMap;
import com.iemes.entity.CraftprocessFormMap;
import com.iemes.entity.CraftthreeTimeFormMap;
import com.iemes.entity.CrafttrolleyFormMap;
import com.iemes.entity.CrafttwoTimeFormMap;
import com.iemes.entity.ProcessFormMap;
import com.iemes.entity.ShapeMachineFormTypeMap;
import com.iemes.mapper.BrandMapper;
import com.iemes.mapper.BrandModelMapper;
import com.iemes.mapper.ProcessMapper;
import com.iemes.mapper.client.ClientMapper;
import com.iemes.mapper.craft.CraftfourTimeMapper;
import com.iemes.mapper.craft.CraftoneTimeMapper;
import com.iemes.mapper.craft.CraftplaceMapper;
import com.iemes.mapper.craft.CraftprocessMapper;
import com.iemes.mapper.craft.CraftthreeTimeMapper;
import com.iemes.mapper.craft.CrafttrolleyMapper;
import com.iemes.mapper.craft.CrafttwoTimeMapper;
import com.iemes.mapper.machine.ShapeMachineTypeMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@RequestMapping("/craft/")
@Controller
public class CraftBasicsController extends BaseController{
	@Inject
	private ProcessMapper processMapper;
	@Inject
	private ClientMapper clientMapper;
	@Inject
	private BrandMapper brandMapper;
	@Inject
	private BrandModelMapper brandModelMapper;
	@Inject
	private CraftfourTimeMapper craftfourTimeMapper;
	@Inject
	private CraftoneTimeMapper craftoneTimeMapper;
	@Inject
	private CraftplaceMapper craftplaceMapper;
	@Inject
	private CraftthreeTimeMapper craftthreeTimeMapper;
	@Inject
	private CrafttrolleyMapper crafttrolleyMapper;
	@Inject
	private CraftprocessMapper craftprocessMapper;
	@Inject
	private CrafttwoTimeMapper crafttwoTimeMapper;
	@Inject 
	private ShapeMachineTypeMapper shapeMachineTypeMapper;
	//查看成型工艺数据
	@RequestMapping("molding")
	public String molding(Model model){
		List<ClientFormMap> clientFormMaps=clientMapper.findByWhere(new ClientFormMap());
		model.addAttribute("client", clientFormMaps);
		model.addAttribute("res", findByRes());
		List<BrandFormMap> brandFormMaps=brandMapper.findByWhere(new BrandFormMap());
		List<BrandModelFormMap> brandModelFormMaps=brandModelMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("brand", brandFormMaps);
		model.addAttribute("brandmodel", brandModelFormMaps);
		return Common.BACKGROUND_PATH + "/craft/craft_molding";
	}
	//工艺分页
		@ResponseBody
		@RequestMapping("craft_findByPage")
		public PageView findByPage(String pageNow, String pageSize) 
		{
			//获取所有机器型号
			ShapeMachineFormTypeMap shapeMachineFormTypeMap=getFormMap(ShapeMachineFormTypeMap.class);
			List<ShapeMachineFormTypeMap> shapeMachineFormTypeMaps=shapeMachineTypeMapper.findByWhere(shapeMachineFormTypeMap);
			ProcessFormMap processFormMap=getFormMap(ProcessFormMap.class);
			processFormMap.put("orderby", "order by Enactment_Date");
			processFormMap.put("state", "1");
			processFormMap=toFormMap(processFormMap, pageNow, pageSize,processFormMap.getStr("orderby"));
			List<ProcessFormMap> processFormMaps=processMapper.findByPage(processFormMap);
			for (ProcessFormMap processFormMap2 : processFormMaps) {
				for (ShapeMachineFormTypeMap s : shapeMachineFormTypeMaps) {
					if(processFormMap2.getInt("Machine_model")==s.getInt("id")){
						processFormMap2.put("Machine_typename", s.get("typename"));
					}
				}
			}
			pageView.setRecords(processFormMaps);
			return pageView;
		}
		//查看工艺
		@RequestMapping(value="craft_edit")
		public String craft_edit(Model model) 
		{
			String id = getPara("id");
			if(Common.isNotEmpty(id))
			{
				
				List<ShapeMachineFormTypeMap> shapeMachineFormTypeMaps=shapeMachineTypeMapper.findByWhere(getFormMap(ShapeMachineFormTypeMap.class));
				ProcessFormMap processFormMap = processMapper.findbyFrist("id", id, ProcessFormMap.class);
				for (ShapeMachineFormTypeMap s : shapeMachineFormTypeMaps) {
					if(processFormMap.getInt("Machine_model")==s.getInt("id")){
						processFormMap.put("Machine_typename", s.get("typename"));
					}
				}
				
				CraftfourTimeFormMap craftfourTimeFormMap=craftfourTimeMapper.findbyFrist("id", processFormMap.getStr("fourtime"), CraftfourTimeFormMap.class);
				CraftoneTimeFormMap craftoneTimeFormMap=craftoneTimeMapper.findbyFrist("id",  processFormMap.getStr("onetime"), CraftoneTimeFormMap.class);
				CrafttwoTimeFormMap crafttwoTimeFormMap=crafttwoTimeMapper.findbyFrist("id",  processFormMap.getStr("twotime"), CrafttwoTimeFormMap.class);
				CraftthreeTimeFormMap craftthreeTimeFormMap=craftthreeTimeMapper.findbyFrist("id",  processFormMap.getStr("threetime"), CraftthreeTimeFormMap.class);
				CraftplaceFormMap craftplaceFormMap=craftplaceMapper.findbyFrist("id", processFormMap.getStr("formomg_place"), CraftplaceFormMap.class);
				CrafttrolleyFormMap crafttrolleyFormMap=crafttrolleyMapper.findbyFrist("id",  processFormMap.getStr("formomg_trolley"), CrafttrolleyFormMap.class);
				CraftprocessFormMap craftprocessFormMap=craftprocessMapper.findbyFrist("id",  processFormMap.getStr("forming_process"), CraftprocessFormMap.class);
				model.addAttribute("process", processFormMap);
				model.addAttribute("onetime", craftoneTimeFormMap);
				model.addAttribute("twotime", crafttwoTimeFormMap);
				model.addAttribute("threetime", craftthreeTimeFormMap);
				model.addAttribute("fourtime", craftfourTimeFormMap);
				model.addAttribute("place", craftplaceFormMap);
				model.addAttribute("trolley", crafttrolleyFormMap);
				model.addAttribute("craftprocess", craftprocessFormMap);
				model.addAttribute("shapemachine",shapeMachineFormTypeMaps);
		}
			if(Integer.valueOf(getPara("state"))==1){
			return Common.BACKGROUND_PATH + "/craft/craft_editUpd";
			}else{
			return Common.BACKGROUND_PATH + "/craft/craft_edit";
			}
		}
		//修改工艺操作
		@ResponseBody
		@RequestMapping("craft_addEntity")
		@SystemLog(module="成型工艺",methods="成型工艺-修改工艺")//凡需要处理业务逻辑的.都需要记录操作日志
		@Transactional
		public String addEntity() throws Exception {
			//修改工艺为添加最新工艺，保存原来工艺，即为新增工艺，保存日期不一样
			ProcessFormMap processFormMap = getFormMap(ProcessFormMap.class);
			ProcessFormMap processFormMapupt=processMapper.findbyFrist("id", processFormMap.getStr("id"), ProcessFormMap.class);
			processFormMapupt.put("state", 0);
			processMapper.editEntity(processFormMapupt);
			processFormMap.remove("id");
			processFormMap.put("Enactment_Date", DateUtils.getStringDateTime());
			CraftfourTimeFormMap craftfourTimeFormMap=getFormMap(CraftfourTimeFormMap.class);
			CraftoneTimeFormMap craftoneTimeFormMap=getFormMap(CraftoneTimeFormMap.class);
			CrafttwoTimeFormMap crafttwoTimeFormMap=getFormMap(CrafttwoTimeFormMap.class);
			CraftthreeTimeFormMap craftthreeTimeFormMap=getFormMap(CraftthreeTimeFormMap.class);
			CraftplaceFormMap craftplaceFormMap=getFormMap(CraftplaceFormMap.class);
			CrafttrolleyFormMap crafttrolleyFormMap=getFormMap(CrafttrolleyFormMap.class);
			CraftprocessFormMap craftprocessFormMap=getFormMap(CraftprocessFormMap.class);
			try {
			craftfourTimeMapper.addEntity(craftfourTimeFormMap);
			processFormMap.put("fourtime",craftfourTimeFormMap.get("id"));
			craftoneTimeMapper.addEntity(craftoneTimeFormMap);
			processFormMap.put("onetime",craftoneTimeFormMap.get("id"));
			crafttwoTimeMapper.addEntity(crafttwoTimeFormMap);
			processFormMap.put("twotime",crafttwoTimeFormMap.get("id"));
			craftthreeTimeMapper.addEntity(craftthreeTimeFormMap);
			processFormMap.put("threetime",craftthreeTimeFormMap.get("id"));
			craftplaceMapper.addEntity(craftplaceFormMap);
			processFormMap.put("formomg_place",craftplaceFormMap.get("id"));
			crafttrolleyMapper.addEntity(crafttrolleyFormMap);
			processFormMap.put("formomg_trolley",crafttrolleyFormMap.get("id"));
			craftprocessMapper.addEntity(craftprocessFormMap);
			processFormMap.put("forming_process",craftprocessFormMap.get("id"));
			processFormMap.put("state", 1);
			processMapper.addEntity(processFormMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return "false";
			}
			return "success";
		}
		//删除工艺操作
				@ResponseBody
				@RequestMapping("craft_deleteEntity")
				@Transactional(readOnly=false)//需要事务操作必须加入此注解
				@SystemLog(module="成型管理",methods="成型管理-删除工艺")//凡需要处理业务逻辑的.都需要记录操作日志
				public String deleteEntity() {
						String[] ids = getParaValues("ids");
						String Pid=null;
						for (String id : ids) {
							Pid=id;
							//orderMapper.deleteOrder(Integer.valueOf(id));
						}
						String[] value=Pid.split(",");
						for (String string : value) {
							processMapper.deleteCraft(Integer.valueOf(string));
						}
						return "success";
				}
}
